package com.mijie.homi.admin.service.commom;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mijie.core.ApplicationProperties;
import com.mijie.homi.admin.dao.commom.NickNameBoxDao;
import com.mijie.homi.admin.domain.commom.NickNameBox;

@Service
public class NickNameBoxService {

	@Autowired
	private NickNameBoxDao dao;
	private List<NickNameBox> cache;
	private static int FETCH_SIZE = 200;
	private int cacheIndex = 0;
	
	private  List<NickNameBox> getNickNames(){
		if(cache==null || cache.isEmpty()){
			cache = dao.listNickNameBox(FETCH_SIZE);
		}
		return cache;
	}
	
	public synchronized NickNameBox getFreeNickNameBox(){
		List<NickNameBox> list = getNickNames();
		NickNameBox box = null;
		if(!list.isEmpty()){
			box = list.get(cacheIndex++);
			if(cacheIndex==FETCH_SIZE){
				cacheIndex = 0;
				cache.clear();
			}
		}
		return box;
	}
	
	public int removeNickNameBox(int id){
		return dao.removeNickNameBox(id);
	}
}
